﻿$(function () {


});

$(document).ready(function () {
    var glasswrapper = $('#glasswrapper');
    var glass = $('#DialogGlass');
    glass.css({ height: $('#site').height(), width: $('#site').width() });
    glasswrapper.hide();
    $("label").inFieldLabels();
    $("button").button();
    $("tabarea").tabs();
    $('#expireWarningWrapper').dialog({ autoOpen: false, minWidth: 475, minHeight: 140, resizable: false, close: function (event, ui) { glasswrapper.hide(); } })

    //            $('#sliderecent').tabSlideOut({
    //                tabHandle: '#recenthandle',                              //class of the element that will be your tab
    //                pathToTabImage: '../images/slidetab.png',          //path to the image for the tab (optionaly can be set using css)
    //                imageHeight: '40px',                               //height of tab image
    //                imageWidth: '55px',                               //width of tab image    
    //                tabLocation: 'left',                               //side of screen where tab lives, top, right, bottom, or left
    //                speed: 300,                                        //speed of animation
    //                action: 'click',                                   //options: 'click' or 'hover', action to trigger animation
    //                topPos: '220px',                                   //position from the top
    //                fixedPosition: false                               //options: true makes it stick(fixed position) on scroll
    //            });

    var timerlength = 20;

    //methods
    checkTimer = function () {
        timerlength--;
        if (timerlength <= 3) {
            checkSession();
        }
        if (timerlength <= 1) {
            //show pop up
            showWarning();
        }
        if (timerlength <= 0) {
            timeout();
        }
        if (timerlength > 3) {
            hideWarning();
        }
    }

    showWarning = function () {
        var glass = $('#DialogGlass');
        glass.css({ height: $('#site').height(), width: $('#site').width() });
        glasswrapper.show();
        $('#expireWarningWrapper').dialog('open');
        $('#btn_continue').focus();
    }

    hideWarning = function () {
        if ($('#expireWarningWrapper').dialog("isOpen")) {
            $('#expireWarningWrapper').dialog('close');
        }
    }

    checkSession = function () {
        var hidsearchid = $('#hidsearchid').val();
        var paramstring = '{seid:"' + hidsearchid + '"}';
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/SessionCheck",
            data: paramstring,
            dataType: "json",
            success: function (data, textStatus) {
                //timerlength=whatever is returned minus 13 minutes
                var ret = jQuery.parseJSON(data.d);
                timerlength = ret.Message;
            },
            error: function (data, textStatus) {
                //go ahead and log out
                //alert('An error has occured retrieving data!');
            }
        });
    }

    extendSession = function () {
        var hidsearchid = $('#hidsearchid').val();
        var paramstring = '{seid:"' + hidsearchid + '"}';

        //remove these two lines
        timerlength = 20;
        hideWarning();

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Service/Services.aspx/SessionExtend",
            data: paramstring,
            dataType: "json",
            success: function (data, textStatus) {
                timerlength = 20;
            },
            error: function (data, textStatus) {
                //alert('An error has occured retrieving data!');
            }
        });
    }

    logout = function () {
        window.location = "../default.aspx?status=logout";
    }

    timeout = function () {
        window.location = "../default.aspx?status=timedout";
    }

    startTimer = function () {
        setInterval(checkTimer, 60000);
    }

    startTimer();
});

function logOut() {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: '/Service/Services.aspx/LogOut',
        async: false,
        dataType: "json",
        success: function (data, textStatus) {
            data = jQuery.parseJSON(data.d);
            if (data.logOutResult) {
                window.location = "../Default.aspx";
            }
        },
        error: function (data, textStatus) {

        }
    });
}
